package com.itheima.reggie;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 解决线程原子问题
 */
public class AtomicInterTest23 {
    static AtomicInteger number = new AtomicInteger();

    public static void main(String[] args) {
        Run3 run3 = new Run3();
        run3.setName("线程三号");
        run3.start();


        for (int i = 0; i < 200000; i++) {
            number.getAndIncrement();
        }

        //增加耗时操作确保主线程和子线程共享变量自增而是万次
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.err.println(Thread.currentThread().getName()
                + "对当前变量进行累加的数据" + number.get());



    }
}
class Run3 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 200000; i++) {
            AtomicInterTest23.number.getAndIncrement();
        }
        System.err.println("当前线程+"+Thread.currentThread().getName()
                +"对变量进行的累加数据"+AtomicInterTest23.number.get());
    }
}

/**
 * volatile  为什么不能解决原子性问题
 */
